namespace Plugins.Serializer
{
    using System;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Security;
    using Plugins.Interfaces;
    using Plugins.Library;

    [Serializable]
    public class PluginIsolatedStorageSerializer<T> : PluginFileStreamSerializer<T>
    {
        protected override string GetFileName(string fileName)
        {
            lock (((PluginIsolatedStorageSerializer<T>) this))
            {
                try
                {
                    string[] files = IsolatedStorageFile.GetUserStoreForApplication().GetFileNames(fileName);
                    if ((files != null) && (files.Length == 1))
                    {
                        return files[0];
                    }
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (SecurityException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return null;
            }
        }

        protected override bool Deserialize(string fileName, IPluginTypeFormatter formatter)
        {
            lock (((PluginIsolatedStorageSerializer<T>) this))
            {
                try
                {
                    IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                    using (Stream stream = new IsolatedStorageFileStream(GetFileName(fileName), FileMode.Open, FileAccess.Read, FileShare.None, file))
                    {
                        return Deserialize(stream, formatter);
                    }
                }
                catch (FileNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (IsolatedStorageException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return false;
            }
        }

        protected override bool Serialize(string fileName, T obj, IPluginTypeFormatter formatter)
        {
            lock (((PluginIsolatedStorageSerializer<T>) this))
            {
                try
                {
                    IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                    using (Stream stream = new IsolatedStorageFileStream(GetFileName(fileName), FileMode.Create, FileAccess.Write, FileShare.None, file))
                    {
                        return Serialize(obj, stream, formatter);
                    }
                }
                catch (FileNotFoundException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentNullException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (ArgumentException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                catch (IsolatedStorageException ex)
                {
                    PluginLog.WriteErrorSync(ex);
                }
                return false;
            }
        }
    }
}

